/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package io.iec.edp.caf.rpc.config;

import io.iec.edp.caf.commons.event.config.EventListenerData;
import io.iec.edp.caf.commons.event.config.EventListenerSettings;
import io.iec.edp.caf.commons.event.config.EventManagerData;
import io.iec.edp.caf.rpc.api.event.RpcClientEventBroker;
import io.iec.edp.caf.rpc.api.event.RpcClientEventManager;
import io.iec.edp.caf.rpc.api.event.RpcServerEventBroker;
import io.iec.edp.caf.rpc.api.local.InternalServiceManageServiceImpl;
import io.iec.edp.caf.rpc.api.properties.RPCConfigurationProperties;
import io.iec.edp.caf.rpc.api.service.InternalServiceManageService;
import io.iec.edp.caf.rpc.api.service.RpcClient;
import io.iec.edp.caf.rpc.client.RpcClassHolder;
import io.iec.edp.caf.rpc.client.RpcClientImpl;
import io.iec.edp.caf.rpc.client.local.RpcLocalInvoker;
import io.iec.edp.caf.rpc.client.local.RpcLocalInvokerImpl;
import io.iec.edp.caf.rpc.server.invoker.RpcRemoteInvoker;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({
        RPCConfigurationProperties.class
})
public class RpcClientAutoConfiguration {

    /**
     * InternalSvrManagement Bean
     *
     * @return
     */
    @Bean
    public InternalServiceManageService createInternalSvrManagement() {
        return new InternalServiceManageServiceImpl();
    }

    /**
     * RpcClient的bean
     * @return
     * @
     */
//    @Bean
//    public RpcClient createRpcClient(InternalServiceManageService management, RpcLocalInvoker rpcLocalInvoker, RpcRemoteInvoker rpcRemoteInvoker, ServiceUnitAwareService serviceUnitAware, ITenantService tenantService, ITenantRouteService tenantRouteService,RpcAddressDiscover rpcAddressDiscover) {
//        return new RpcClientImpl(management, rpcLocalInvoker, rpcRemoteInvoker, serviceUnitAware,tenantService, tenantRouteService,rpcAddressDiscover);
//    }
    @Bean
    public RpcClient createRpcClient(InternalServiceManageService management, RpcLocalInvoker rpcLocalInvoker, RpcRemoteInvoker rpcRemoteInvoker) {
        return new RpcClientImpl(management, rpcLocalInvoker, rpcRemoteInvoker);
    }


    @Bean
    public RpcClientEventManager createRpcClientEventManager(){
        return new RpcClientEventManager();
    }

    @Bean
//    @ConditionalOnBean(EventListenerSettings.class)
    public RpcClientEventBroker createRpcClientEventBroker(RpcClientEventManager manager, EventListenerSettings settings){
        return new RpcClientEventBroker(manager,settings);
    }


    @Bean
    public RpcClassHolder rpcClassHolder(RpcClient rpcClient) {
        return new RpcClassHolder(rpcClient);
    }


//    @Bean
//    public RpcLocalInvoker rpcLocalInvoker(RpcClientEventBroker clientEventBroker,
//                                           RpcServerEventBroker serverEventBroker,
//                                           ICAFContextService cafContextService,
//                                           RPCConfigurationProperties properties) {
//        return new RpcLocalInvokerImpl(clientEventBroker,serverEventBroker,cafContextService,properties);
//    }
@Bean
public RpcLocalInvoker rpcLocalInvoker(RpcClientEventBroker clientEventBroker,
                                       RpcServerEventBroker serverEventBroker,
                                       RPCConfigurationProperties properties) {
    return new RpcLocalInvokerImpl(clientEventBroker,serverEventBroker,properties);
}

    @Bean
    public EventManagerData createRpcClientManagerData() {
        //声明EventManager
        EventManagerData managerData = new EventManagerData();
        //为EventManager设置名称
        managerData.setName("RpcClientEventManager");
        //声明EventListener
        EventListenerData listenerData = new EventListenerData();
        //为EventListener设置名称
        listenerData.setName("RpcClientLoggingListener");
        //设置步骤1中实现类的类名
        listenerData.setImplClassName("io.iec.edp.caf.rpc.client.listener.RpcClientLoggingListener");
        List<EventListenerData> listeners = new ArrayList<>();
        listeners.add(listenerData);
        //将Listener注册到EventManager
        managerData.setListeners(listeners);
        return managerData;
    }

}
